package handling.login;

import client.MapleClient;
import configs.Game;
import constants.ServerConfig;
import handling.mina.ServerConnection;
import tools.FileoutputUtil;
import tools.Pair;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

public class LoginServer {
    public static short port;
    private static ServerConnection acceptor;
    private static Map<Integer, Integer> load;
    private static int usersOn;
    private static boolean finishedShutdown;
    private static Map<Integer, String> LoginMacs;
    private static Map<Integer, String> LoginKey;
    private static Map<Integer, String> ClientKey;
    private static Map<Integer, String> ServerKey;
    private static AccountStorage clients;
    private static Map<Integer, Long> ChangeChannelTime;
    private static Map<Integer, Long> EnterGameTime;
    private static Map<Integer, Pair<Integer, Integer>> chrpos;

    public static void addChannel(int channel) {
        LoginServer.load.put(Integer.valueOf(channel), Integer.valueOf(0));
    }

    public static void removeChannel(int channel) {
        LoginServer.load.remove(Integer.valueOf(channel));
    }

    public static void setup() {
        LoginServer.port = Game.服务端端口;
        (LoginServer.acceptor = new ServerConnection((int) LoginServer.port, 0, 0)).run();
        System.out.println("[" + FileoutputUtil.CurrentReadable_Time() + "][信息]:登入服务器 - 监听端口: " + Short.toString(LoginServer.port));
    }

    public static void shutdown() {
        if (LoginServer.finishedShutdown) {
            System.out.println("【登入服务器】 已经关闭了...无法执行此动作");
            return;
        }
        System.out.println("[" + FileoutputUtil.CurrentReadable_Time() + "][信息]:[登入服务器端口] 正在解除端口");
        LoginServer.acceptor.close();
        System.out.println("[" + FileoutputUtil.CurrentReadable_Time() + "][信息]:[登入服务器端口] 解除端口完毕");
        LoginServer.finishedShutdown = true;
    }

    public static String getServerName() {
        return ServerConfig.SERVER_NAME;
    }

    public static Map<Integer, Integer> getLoad() {
        return LoginServer.load;
    }

    public static void setLoad(Map<Integer, Integer> load_, int usersOn_) {
        LoginServer.load = load_;
        LoginServer.usersOn = usersOn_;
    }

    public static int getUsersOn() {
        return LoginServer.usersOn;
    }

    public static boolean isShutdown() {
        return LoginServer.finishedShutdown;
    }

    public static void setOn() {
        LoginServer.finishedShutdown = false;
    }

    public static boolean getAutoReg() {
        return ServerConfig.AUTO_REGISTER;
    }

    public static void setAutoReg(boolean x) {
        ServerConfig.AUTO_REGISTER = x;
    }

    public static String getTouDing() {
        return ServerConfig.TOUDING;
    }

    public static void setTouDing(String x) {
        ServerConfig.TOUDING = x;
    }

    public static boolean getNMGB() {
        return ServerConfig.NMGB;
    }

    public static void setNMGB(boolean x) {
        ServerConfig.NMGB = x;
    }

    public static boolean getPDCS() {
        return ServerConfig.PDCS;
    }

    public static void setPDCS(boolean x) {
        ServerConfig.PDCS = x;
    }

    public static AccountStorage getClientStorage() {
        if (LoginServer.clients == null) {
            LoginServer.clients = new AccountStorage();
        }
        return LoginServer.clients;
    }

    public static void removeClient(MapleClient c) {
        getClientStorage().deregisterAccount(c);
    }

    public static String getLoginMac(MapleClient c) {
        String macs = null;
        if (LoginServer.LoginMacs.containsKey(Integer.valueOf(c.getAccID()))) {
            macs = (String) LoginServer.LoginMacs.get(Integer.valueOf(c.getAccID()));
        }
        return macs;
    }

    public static String removeLoginMac(MapleClient c) {
        String macs = null;
        if (LoginServer.LoginMacs.containsKey(Integer.valueOf(c.getAccID()))) {
            LoginServer.LoginMacs.remove(Integer.valueOf(c.getAccID()));
        }
        return macs;
    }

    public static boolean CanLoginKey(String key, int AccID) {
        return LoginServer.LoginKey.get(Integer.valueOf(AccID)) == null || (LoginServer.LoginKey.containsValue(key) && ((String) LoginServer.LoginKey.get(Integer.valueOf(AccID))).equals(key));
    }

    public static boolean RemoveLoginKey(int AccID) {
        try {
            LoginServer.LoginKey.remove(Integer.valueOf(AccID));
        } catch (Exception ex) {
            FileoutputUtil.outError("logs/移除Key异常.txt", (Throwable) ex);
        }
        return true;
    }

    public static boolean addLoginKey(String key, int AccID) {
        try {
            if (LoginServer.LoginKey.get(Integer.valueOf(AccID)) == null) {
                LoginServer.LoginKey.put(Integer.valueOf(AccID), key);
                return true;
            }
            LoginServer.LoginKey.remove(Integer.valueOf(AccID));
            LoginServer.LoginKey.put(Integer.valueOf(AccID), key);
        } catch (Exception ex) {
            FileoutputUtil.outError("logs/添加Key异常.txt", (Throwable) ex);
        }
        return true;
    }

    public static String getLoginKey(int AccID) {
        return (String) LoginServer.LoginKey.get(Integer.valueOf(AccID));
    }

    public static boolean CanServerKey(String key, int AccID) {
        return LoginServer.ServerKey.get(Integer.valueOf(AccID)) == null || (LoginServer.ServerKey.containsValue(key) && ((String) LoginServer.ServerKey.get(Integer.valueOf(AccID))).equals(key));
    }

    public static boolean RemoveServerKey(int AccID) {
        try {
            LoginServer.ServerKey.remove(Integer.valueOf(AccID));
        } catch (Exception ex) {
            FileoutputUtil.outError("logs/移除Key异常.txt", (Throwable) ex);
        }
        return true;
    }

    public static boolean addServerKey(String key, int AccID) {
        try {
            if (LoginServer.ServerKey.get(Integer.valueOf(AccID)) == null) {
                LoginServer.ServerKey.put(Integer.valueOf(AccID), key);
                return true;
            }
            LoginServer.ServerKey.remove(Integer.valueOf(AccID));
            LoginServer.ServerKey.put(Integer.valueOf(AccID), key);
        } catch (Exception ex) {
            FileoutputUtil.outError("logs/添加Key异常.txt", (Throwable) ex);
        }
        return true;
    }

    public static String getServerKey(int AccID) {
        return (String) LoginServer.ServerKey.get(Integer.valueOf(AccID));
    }

    public static boolean CanClientKey(String key, int AccID) {
        return LoginServer.ClientKey.get(Integer.valueOf(AccID)) == null || (LoginServer.ClientKey.containsValue(key) && ((String) LoginServer.ClientKey.get(Integer.valueOf(AccID))).equals(key));
    }

    public static boolean RemoveClientKey(int AccID) {
        try {
            LoginServer.ClientKey.remove(Integer.valueOf(AccID));
        } catch (Exception ex) {
            FileoutputUtil.outError("logs/移除Key异常.txt", (Throwable) ex);
        }
        return true;
    }

    public static boolean addClientKey(String key, int AccID) {
        try {
            if (LoginServer.ClientKey.get(Integer.valueOf(AccID)) == null) {
                LoginServer.ClientKey.put(Integer.valueOf(AccID), key);
                return true;
            }
            LoginServer.ClientKey.remove(Integer.valueOf(AccID));
            LoginServer.ClientKey.put(Integer.valueOf(AccID), key);
        } catch (Exception ex) {
            FileoutputUtil.outError("logs/添加Key异常.txt", (Throwable) ex);
        }
        return true;
    }

    public static String getClientKey(int AccID) {
        return (String) LoginServer.ClientKey.get(Integer.valueOf(AccID));
    }

    public static boolean getDiscounied() {
        return ServerConfig.DISCOUNTED;
    }

    public static void setDiscounied(boolean x) {
        ServerConfig.DISCOUNTED = x;
    }

    public static int getRSGS() {
        return ServerConfig.RSGS;
    }

    public static void setRSGS(int x) {
        ServerConfig.RSGS = x;
    }

    public static void forceRemoveClient(MapleClient client) {
        forceRemoveClient(client, true);
    }

    public static void forceRemoveClient(MapleClient client, boolean remove) {
        Collection<MapleClient> cls = getClientStorage().getAllClientsThreadSafe();
        for (MapleClient c : cls) {
            if (c == null) {
                continue;
            }
            if (c.getAccID() != client.getAccID() && c != client) {
                continue;
            }
            if (c != client) {
                c.unLockDisconnect();
            }
            if (!remove) {
                continue;
            }
            removeClient(c);
        }
    }

    public static long getLoginAgainTime(int accid) {
        return ((Long) LoginServer.ChangeChannelTime.get(Integer.valueOf(accid))).longValue();
    }

    public static void addLoginAgainTime(int accid) {
        LoginServer.ChangeChannelTime.put(Integer.valueOf(accid), Long.valueOf(System.currentTimeMillis()));
    }

    public static boolean canLoginAgain(int accid) {
        long lastTime = System.currentTimeMillis();
        if (LoginServer.ChangeChannelTime.containsKey(Integer.valueOf(accid))) {
            long lastSelectCharTime = ((Long) LoginServer.ChangeChannelTime.get(Integer.valueOf(accid))).longValue();
            if (lastSelectCharTime + 40000L > lastTime) {
                return false;
            }
        }
        return true;
    }

    public static long getEnterGameAgainTime(int accid) {
        return ((Long) LoginServer.EnterGameTime.get(Integer.valueOf(accid))).longValue();
    }

    public static void addEnterGameAgainTime(int accid) {
        LoginServer.EnterGameTime.put(Integer.valueOf(accid), Long.valueOf(System.currentTimeMillis()));
    }

    public static boolean canEnterGameAgain(int accid) {
        long lastTime = System.currentTimeMillis();
        if (LoginServer.EnterGameTime.containsKey(Integer.valueOf(accid))) {
            long lastSelectCharTime = ((Long) LoginServer.EnterGameTime.get(Integer.valueOf(accid))).longValue();
            if (lastSelectCharTime + 60000L > lastTime) {
                return false;
            }
        }
        return true;
    }

    public static void setChrPos(Map<Integer, Pair<Integer, Integer>> keys) {
        LoginServer.chrpos = keys;
    }

    public static Map<Integer, Pair<Integer, Integer>> getChrPos() {
        return LoginServer.chrpos;
    }

    public static void RemoveChrPos(int chrid) {
        LoginServer.chrpos.remove(Integer.valueOf(chrid));
    }

    static {
        port = 18484;
        load = (Map<Integer, Integer>) new HashMap();
        usersOn = 0;
        finishedShutdown = true;
        LoginMacs = new WeakHashMap();
        LoginKey = new HashMap();
        ClientKey = new HashMap();
        ServerKey = new HashMap();
        ChangeChannelTime = new HashMap();
        EnterGameTime = new HashMap();
        chrpos = (Map<Integer, Pair<Integer, Integer>>) new HashMap();
    }
}
